package com.llb666.shop.user.infrastructure.persistence;


import com.llb666.shop.user.domain.entity.Users;
import com.llb666.shop.user.domain.repository.UserRepository;
import com.llb666.shop.user.infrastructure.conver.UsersCvt;
import com.llb666.shop.user.infrastructure.mapper.UsersMapper;
import com.llb666.shop.user.infrastructure.po.UsersPo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

/**
 * 用户仓储层实现类.
 *
 * @author llb666
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class UserRepositoryImpl implements UserRepository {

    private final UsersMapper usersMapper;

    @Override
    public Boolean queryUsernameIsExist(String username) {
        Example example = new Example(UsersPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", username);
        UsersPo result = usersMapper.selectOneByExample(example);
        return result != null;
    }

    @Override
    public Users login(String username, String password) {
        Example example = new Example(UsersPo.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username", username);
        criteria.andEqualTo("password", password);
        return UsersCvt.INSTANCE.po2do(usersMapper.selectOneByExample(example));
    }

    @Override
    public void insertSelective(Users user) {
        usersMapper.insertSelective(UsersCvt.INSTANCE.do2po(user));
    }
}
